library Wave5 initializer init requires LegacySystem

    globals
        private trigger ConvoyRouteTrigger
        private trigger ConvoyLossTrigger
        private trigger ConvoyExfiltrationTrigger
        private trigger ConvoyGuardAITrigger
		
		private rect array              udg_ConvoyPath
		
    endglobals
  
    private function ExecuteWave6 takes nothing returns nothing
        //call DestroyTrigger(gg_trg_Civilian_AI)
        call ExecuteRegisteredFunction("Wave0.CleanupLeaks")
        call ExecuteRegisteredFunction("Wave6a")
        //call Wave6a.execute()
        call DisableTrigger(ConvoyRouteTrigger)
        call DisableTrigger(ConvoyLossTrigger)
        call DisableTrigger(ConvoyGuardAITrigger)
        call TriggerSleepAction(30.0)
        call DisableTrigger(ConvoyExfiltrationTrigger)
    endfunction
    
    private function InitCaravanPathEvents takes nothing returns nothing
		local integer A = 1
		loop
			exitwhen A>udg_nConvoyPoints
			call TriggerRegisterEnterRectSimple(ConvoyRouteTrigger,udg_ConvoyPath[A])
			set A=A+1
		endloop
	endfunction

	private function CPathOrder takes nothing returns nothing
		local real x = GetRandomReal(GetRectMinX(udg_ConvoyPath[udg_ConvoyProgress]),GetRectMaxX(udg_ConvoyPath[udg_ConvoyProgress]))
		local real y = GetRandomReal(GetRectMinY(udg_ConvoyPath[udg_ConvoyProgress]),GetRectMaxY(udg_ConvoyPath[udg_ConvoyProgress]))
		local unit u = GetEnumUnit()

		if GetUnitPointValue(u)==12 and GetRandomInt(0,1)==1 then
			call IssuePointOrder(u,"patrol",x,y)
		else
			call IssuePointOrder(u,"move",x,y)
		endif

		set u = null
	endfunction

	private function CPathEvac takes nothing returns nothing
		local unit u = GetEnumUnit()
		call ItemAI(u)
		call IssuePointOrder( u, "attack", GetRandomReal(GetRectMinX(udg_ConvoyPath[udg_ConvoyProgress]), GetRectMaxX(udg_ConvoyPath[udg_ConvoyProgress])), GetRandomReal(GetRectMinY(udg_ConvoyPath[udg_ConvoyProgress]), GetRectMaxY(udg_ConvoyPath[udg_ConvoyProgress])) )
		set u = null
	endfunction

	private function CPathGatherE takes unit u, boolean sleep returns nothing
		local integer squadE = udg_SquadEnum
		if sleep then
			call TriggerSleepAction(GetRandomReal(0.1,0.7))
		endif

		if GetUnitPointValue(u)==12 then
			call IssueImmediateOrder(u,"berserk")
		endif
		call IssuePointOrder( u, "move", GetUnitX(udg_SquadLeader[squadE])+10.0, GetUnitY(udg_SquadLeader[squadE])+10.0 )
		
		if not sleep and GetUnitTypeId(u)=='z001' and GetRandomInt(0,3) > 0 then
			call CorpsmanAI.execute(u)
		endif
	endfunction
	private function CPathGather takes nothing returns nothing
		if GetRandomInt(0,1)==1 then
			call CPathGatherE.execute(GetEnumUnit(), true)
		else
			call CPathGatherE.evaluate(GetEnumUnit(), false)
		endif
	endfunction

	
	private function CPathGuardE takes unit u, boolean sleep returns nothing
		if sleep then
			call TriggerSleepAction(GetRandomReal(0.1,0.7))
		endif
		call ItemAI(u)
		call IssuePointOrder( u, "attack", GetRandomReal(GetRectMinX(udg_ConvoyPath[udg_ConvoyProgress]), GetRectMaxX(udg_ConvoyPath[udg_ConvoyProgress])), GetRandomReal(GetRectMinY(udg_ConvoyPath[udg_ConvoyProgress]), GetRectMaxY(udg_ConvoyPath[udg_ConvoyProgress])) )
		
		if not sleep and GetUnitTypeId(u)=='z001' and GetRandomInt(0,3) > 0 then
			call CorpsmanAI.execute(u)
		endif
	endfunction
	private function CPathGuard takes nothing returns nothing
		if GetRandomInt(0,1)==1 then
			call CPathGuardE.execute(GetEnumUnit(), true)
		else
			call CPathGuardE.evaluate(GetEnumUnit(), false)
		endif
	endfunction

	private function ConvoyRouteConditions takes nothing returns boolean
        return GetUnitTypeId(GetTriggerUnit())=='hrdh'
    endfunction
    private function ConvoyRoute takes nothing returns nothing
		local group ConvoyGroup = GetUnitsOfPlayerAll(Player(9))
		local group g
		local unit u
		call PolledWait(0.75)
		set g = GetUnitsInRectOfPlayer(udg_ConvoyPath[udg_ConvoyProgress],Player(9))
		set u = FirstOfGroup(g)
		if u!=null then
			set u = null
			if udg_nConvoyPoints>udg_ConvoyProgress then
				set udg_ConvoyProgress = udg_ConvoyProgress + 1
			endif
			call PolledWait(3.)
			call ForGroup(ConvoyGroup,function CPathOrder)
			call ForGroup(udg_InfantryGroup[1],function CPathOrder)
		endif
		call DestroyGroup(g)
		set g = null
		call DestroyGroup(ConvoyGroup)
		set ConvoyGroup = null
    endfunction
	
    private function ErisEncounterCOne takes nothing returns nothing
        local location l = GetUnitLoc(udg_Boss)
        local integer i
        call DestroyTrigger(GetTriggeringTrigger())
        call SetNVGEnabled(false)
        call PauseUnit(udg_Boss,true)
        set udg_Lighting = true
        set udg_LZPoint = GetUnitLoc(udg_Boss)
        call BlackHawkWashOff.execute()
        set udg_soundpath = "Abilities\\Spells\\Undead\\Sleep\\SleepBirth1.wav"
        call PlaySoundPath.execute()
        call CinematicFilterGenericBJ( 2.0, BLEND_MODE_BLEND, "ReplaceableTextures\\CameraMasks\\White_mask.blp", 0.00, 0, 0.00, 100, 60.0, 0.00, 0.00, 40.00 )
        call TriggerSleepAction(0.5)
        call PauseUnit(udg_Boss,false)
        call RemoveLocation(l)
        set l = null
        call PolledWait(3.)
        call RemoveLocation(udg_LZPoint)
    endfunction

    private function ErisKilledCOne takes nothing returns nothing
        local integer i = GetRandomInt(1, udg_Players)
        call DestroyTrigger(GetTriggeringTrigger())
        set udg_Lighting = false
        call PolledWait(2.)
        call SetNVGEnabled(true)
        call IntermissionRestore.execute()
    endfunction

    private function EtaphrevenSpawn takes nothing returns nothing
        local location l
        local trigger t
        call PolledWait(GetRandomReal(1.0,200.0))
        set l = GetRandomLocInRect(udg_UndeadRandomSpawn[GetRandomInt(1,4)])
        if udg_SquadXP>70000 and udg_Players>4 and GetRandomInt(0,10)==1 then
            set udg_Boss = CreateUnitAtLoc(Player(13),'U007',l,0) //Eris
            set t = CreateTrigger()
            call TriggerRegisterUnitEvent( t, udg_Boss, EVENT_UNIT_ATTACKED )
            call TriggerAddAction( t, function ErisEncounterCOne )
            set t = CreateTrigger()
            call TriggerRegisterUnitEvent( t, udg_Boss, EVENT_UNIT_DEATH )
            call TriggerAddAction( t, function ErisKilledCOne )
            set t = null
        elseif udg_SquadXP>40000 and udg_Players>4 then
            set udg_Boss = CreateUnitAtLoc(Player(13),'U00A',l,0)
        else
            set udg_Boss = CreateUnitAtLoc(Player(13),'Uvar',l,0)
        endif
        call SetUnitColor(udg_Boss,PLAYER_COLOR_BROWN)
        call SuspendHeroXP(udg_Boss,true)
        if GetRandomInt(1,10)==1 then
            if GetRandomInt(1,2)==1 then
                call UnitAddAbility(udg_Boss,'A008')
            else
                call UnitAddAbility(udg_Boss,'Apiv')
            endif
        endif
        if udg_Mode==2 then
            call GeneralText(10.0, "|cfff3ad00Major Grimes|r : Etaphreven has been spotted in your sector!" )
        endif
        call RemoveLocation(l)
        set l = null
    endfunction

    private function ConvoyStart takes nothing returns nothing
        call IssuePointOrder(GetEnumUnit(),"move",GetRectCenterX(udg_ConvoyPath[1]),GetRectCenterY(udg_ConvoyPath[1]))
    endfunction

    private function CiviliansToConvoy takes nothing returns nothing
        local unit u = GetEnumUnit()
        call IssuePointOrder(u,"move",-7000.0,5900.0)
        call GroupAddUnitSimple(u,udg_InfantryGroup[2])
        set u = null
    endfunction

    private function CiviliansSpeedDown takes nothing returns nothing
		local unit u = GetEnumUnit()
        call ShiftMoveSpeed(u,-90)
		
		if GetUnitAbilityLevel(u, 'Abun') > 0 then
			call UnitRemoveAbility(u, 'Abun')
		endif
		set u = null
    endfunction

    private function Wave5a takes nothing returns nothing
        local location l
        local integer i = 1
        local integer i2 = 0 //Civilian count
        local unit u
        local group g
        local integer marines
        local integer north = 0
        set udg_CurrentWave = 5
        call GenericActions.execute()
        call EtaphrevenSpawn.execute()
        call TriggerRegisterPlayerUnitEventSimple( ConvoyLossTrigger, Player(9), EVENT_PLAYER_UNIT_DEATH )
        call SetDynamicEvasionEnabled(true)
        set g = GetUnitsOfPlayerAll(Player(9))
        set i2 = CountUnitsInGroup(g)
        call GeneralText(6.0, "|cffcd950cBattalion|r : We've had communications problems with India Company. They don't know what's going on in your sector." )
        call PolledWait(5.)
        call TriggerRegisterEnterRectSimple(ConvoyExfiltrationTrigger,gg_rct_CaravanEnd)
        call TriggerRegisterEnterRectSimple(ConvoyExfiltrationTrigger,gg_rct_CaravanCEnd)
        call SetPlayerHandicap(Player(13),GetPlayerHandicap(Player(13))+0.1)
        call SetPlayerHandicap(Player(14),GetPlayerHandicap(Player(14))+0.1)
        set udg_XPFactor = udg_XPFactor+0.02
        call RandomSpawn('u00L',4,udg_Spawn) //Blink
        call GeneralText(6.0, "|cffcd950cBattalion|r : Their convoy will be passing through from the west. Rendezvous with them and get them up to speed." )
        call PolledWait(4.)
        if i2>0 then
            call GeneralText(4.0, "|cfff3ad00Marine|r : Wilco. We'll bring the civilians to the convoy. It's the only safe way to get them out." )
            call PolledWait(5.)
            call GeneralText(4.0, "|cfff3ad00Marine|r : Let's move out! Escort the civilians to the convoy rendezvous point." )
            call PolledWait(4.)
            call FieldChat(udg_Ogilvy, 5.0, "|cfff3ad00Ogilvy|r : Come on, we must leave Ravenholm. It's not safe for us anymore." )
            call PolledWait(2.)
        endif
        call ForGroup(g,function CiviliansToConvoy)
        if GetWidgetLife(udg_GlobalUnit[100])>0.405 then
            call IssuePointOrder(udg_GlobalUnit[100],"attack",-7000.0,5900.0)
            call GroupAddUnitSimple(udg_GlobalUnit[100],udg_InfantryGroup[1])
        endif
        if GetWidgetLife(udg_GlobalUnit[101])>0.405 then
            call IssuePointOrder(udg_GlobalUnit[101],"attack",-7000.0,5900.0)
            call GroupAddUnitSimple(udg_GlobalUnit[101],udg_InfantryGroup[1])
        endif
        call MultiboardSetTitleText( udg_SquadStatus, "Rendezvous with India Company. Provide Security." )
        call StartSound(gg_snd_SatelliteImaging)
        call PingMinimapEx(-7900.0, 5800.0, 10.0, 255, 0, 0, false)
        call SetCameraQuickPosition(-7900.0, 5800.0)
        call PolledWait(3.)
        call StartSound(gg_snd_VillagerCry)
        call PolledWait(3.)
        call GeneralText(4.0, "|cfff3ad00Marine|r : Just stay calm folks, we'll get you out of here." )
        call ForGroup(g,function CiviliansSpeedDown)
        call DestroyGroup(g)
        call PolledWait(8.)
        call MBSet( udg_SquadStatus, 1, 1, "Objective : Escort Convoy" )
        call QuestSetDescription(udg_CurrentOrders, "Rendezvous with India Company and provide a SITREP. Facilitate convoy escort. The convoy will take one of the main routes eastwards and you should anticipate heavy attacks coming at any moment. |n|nStay close with the convoy and maintain a tight security perimeter." )
        call ALICE2Text(5.0, "|cff8b864eNew Objective|r : Escort Convoy." )
        call SetPlayerName( Player(9), "Convoy" )
        call MissionUpdate.execute()
        call PolledWait(2.)
        set u = CreateUnit(Player(9),'hrdh',-8000.0,5889.0,0.0)
        call RemoveGuardPosition(u)
        call TriggerSleepAction(0.)
        set u = CreateUnit(Player(9),'hrdh',-7966.0,5783.0,0.0)
        call RemoveGuardPosition(u)
        call TriggerSleepAction(0.)
        set u = CreateUnit(Player(9),'hrdh',-7952.0,5668.0,0.0)
        call RemoveGuardPosition(u)
        call TriggerSleepAction(0.)
        set u = CreateUnit(Player(9),'hrdh',-7838.0,5678.0,0.0)
        call RemoveGuardPosition(u)
        call TriggerSleepAction(0.)
        set u = CreateUnit(Player(9),'hrdh',-7811.0,5781.0,0.0)
        call RemoveGuardPosition(u)
        call TriggerSleepAction(0.)
        set udg_SquadLeader[1] = GroupPickRandomUnit(GetUnitsOfTypeIdAll('hrdh'))
        set udg_AITarget = GroupPickRandomUnit(GetUnitsOfTypeIdAll('hrdh'))
        if udg_SquadXP>122000 then
            set marines=udg_Spawn
        elseif udg_SquadXP>71000 then
            set marines=udg_Spawn+1
        else
            set marines=udg_Spawn+2
        endif
        loop
            exitwhen i>marines
            set l = GetRandomLocInRect(gg_rct_West_Road)
            set u = CreateUnit(Player(11),'z008',GetLocationX(l),GetLocationY(l),0.0) //Marine
            call UnitAddItemById(u,'I001') //Composite
            call GroupAddUnitSimple(u,udg_InfantryGroup[1])
            call SetLoadState(I2S(GetHandleId(u)),1)
            call RemoveLocation(l)
            call TriggerSleepAction(0.)
            set i=i+1
        endloop
        set l = GetRandomLocInRect(gg_rct_West_Road)
        set u = CreateUnit(Player(11),'z001',GetLocationX(l),GetLocationY(l),0.0) //Corpsman
        call UnitAddItemById(u,'I004')
        call UnitAddItemById(u,'phea')
        call UnitAddItemById(u,'I001') //Composite
        call GroupAddUnitSimple(u,udg_InfantryGroup[1])
        call SetLoadState(I2S(GetHandleId(u)),1)
        if udg_Players>5 and udg_SquadXP<60000 then
            set u = CreateUnit(Player(11),'z001',GetLocationX(l),GetLocationY(l),0.0) //Corpsman
            call UnitAddItemById(u,'I004')
            call UnitAddItemById(u,'phea')
            call UnitAddItemById(u,'I001') //Composite
            call GroupAddUnitSimple(u,udg_InfantryGroup[1])
            call SetLoadState(I2S(GetHandleId(u)),1)
        endif
        call TriggerSleepAction(0.)
        set u = CreateUnit(Player(11),'z009',GetLocationX(l),GetLocationY(l),0.0) //Flamethrower
        call UnitAddItemById(u,'I004')
        call UnitAddItemById(u,'phea')
        call UnitAddItemById(u,'I005') //Reactive
        call GroupAddUnitSimple(u,udg_InfantryGroup[1])
        call SetLoadState(I2S(GetHandleId(u)),1)
        call TriggerSleepAction(0.)
        set udg_DeltaLeader = CreateUnit(Player(11),'z00B',GetLocationX(l),GetLocationY(l),0.0) //Lieutenant Rasczak
        call UnitAddItemById(udg_DeltaLeader,'I004')
        call UnitAddItemById(udg_DeltaLeader,'phea')
        call UnitAddItemById(udg_DeltaLeader,'I002')
        call UnitAddItemById(udg_DeltaLeader,'I001') //Composite
        call SetUnitUserData(udg_DeltaLeader,69)
        call GroupAddUnitSimple(udg_DeltaLeader,udg_InfantryGroup[1])
        call SetLoadState(I2S(GetHandleId(udg_DeltaLeader)),1)
        call SetSmartGrenadesEnabled(true)
        call RemoveLocation(l)
        set u = null
        set l = null
		call EnableTrigger(ConvoyGuardAITrigger)
        call LightningAmbience.execute()
        call SetForceAllianceStateBJ(bj_FORCE_PLAYER[9],udg_EchoCompany,bj_ALLIANCE_ALLIED_VISION)
        call SetForceAllianceStateBJ(bj_FORCE_PLAYER[11],udg_EchoCompany,bj_ALLIANCE_ALLIED_VISION)
        call SetForceAllianceStateBJ(bj_FORCE_PLAYER[9],udg_EchoCasualties,bj_ALLIANCE_ALLIED_VISION)
        call SetForceAllianceStateBJ(bj_FORCE_PLAYER[11],udg_EchoCasualties,bj_ALLIANCE_ALLIED_VISION)
        if GetRandomInt(1,3)==1 then
            // Mountains Path
            call PolledWait(9.)
            set udg_ConvoyPath[1] = gg_rct_CaravanC1
            set udg_ConvoyPath[2] = gg_rct_CaravanC2
            set udg_ConvoyPath[3] = gg_rct_CaravanC3
            set udg_ConvoyPath[4] = gg_rct_CaravanC4
            set udg_ConvoyPath[5] = gg_rct_CaravanC5
            set udg_ConvoyPath[6] = gg_rct_CaravanC6
            set udg_ConvoyPath[7] = gg_rct_CaravanC7
            set udg_ConvoyPath[8] = gg_rct_CaravanCIntersection
            set udg_ConvoyPath[9] = gg_rct_CaravanCEnd
            set udg_ConvoyRoute = 1
            set udg_nConvoyPoints = 9
            set north = 1
        elseif GetRandomInt(1,2)==1 then
            // North Jungle Path
            set udg_ConvoyPath[1] = gg_rct_CaravanFirst
            set udg_ConvoyPath[2] = gg_rct_CaravanDiverge
            set udg_ConvoyPath[3] = gg_rct_CaravanA1
            set udg_ConvoyPath[4] = gg_rct_CaravanA2
            set udg_ConvoyPath[5] = gg_rct_CaravanA3
            set udg_ConvoyPath[6] = gg_rct_CaravanA4
            set udg_ConvoyPath[7] = gg_rct_CaravanA5
            set udg_ConvoyPath[8] = gg_rct_CaravanA6
            set udg_ConvoyPath[9] = gg_rct_CaravanConverge
            set udg_ConvoyPath[10] = gg_rct_CaravanPreEnd
            set udg_ConvoyPath[11] = gg_rct_CaravanEnd
            set udg_ConvoyRoute = 2
            set udg_nConvoyPoints = 11
            if GetRandomInt(1,2)==1 then
                // Jungle to Mountains Path
                set udg_ConvoyPath[5] = gg_rct_CaravanAtoC
                set udg_ConvoyPath[6] = gg_rct_CaravanCIntersection
                set udg_ConvoyPath[7] = gg_rct_CaravanCEnd
                set udg_ConvoyRoute = 3
                set udg_nConvoyPoints = 7
            endif
        else
            // South Jungle Path
            set udg_ConvoyPath[1] = gg_rct_CaravanFirst
            set udg_ConvoyPath[2] = gg_rct_CaravanDiverge
            set udg_ConvoyPath[3] = gg_rct_CaravanB1
            set udg_ConvoyPath[4] = gg_rct_CaravanB2
            set udg_ConvoyPath[5] = gg_rct_CaravanB3
            set udg_ConvoyPath[6] = gg_rct_CaravanB4
            set udg_ConvoyPath[7] = gg_rct_CaravanB5
            set udg_ConvoyPath[8] = gg_rct_CaravanB6
            set udg_ConvoyPath[9] = gg_rct_CaravanConverge
            set udg_ConvoyPath[10] = gg_rct_CaravanPreEnd
            set udg_ConvoyPath[11] = gg_rct_CaravanEnd
            set udg_ConvoyRoute = 2
            set udg_nConvoyPoints = 11
        endif
        if north==1 then
            call PolledWait(10.)
        endif
		
		
        call InitCaravanPathEvents()
		
		
        set g = GetUnitsOfPlayerAll(Player(9))
        call ForGroup(g,function ConvoyStart)
        call DestroyGroup(g)
        call GeneralText(3.0, "|cfff3ad00Marine|r : We've got Zulu activity here. Your radio out again?" )
        call PolledWait(5.)
        call FieldChat(udg_DeltaLeader,3.0, "|cfff3ad001Lt. Rasczak|r : Third time this week. We'll need your spare PRC-15." )
        call PolledWait(5.)
        call FieldChat(udg_DeltaLeader,5.0, "|cfff3ad001Lt. Rasczak|r : Zulus..? About time I got a piece of this friggin' war." )
        call PolledWait(5.)
        call GeneralText(5.0, "|cfff3ad00Marine|r : Hope you haven't gone off the deep end, Lieutenant. We're here to provide additional security." )
        call PolledWait(5.)
        call FieldChat(udg_DeltaLeader,5.0, "|cfff3ad001Lt. Rasczak|r : Sounds like a sorry excuse to save your own skins. Just don't get in our way." )
        call PolledWait(4.)
        if i2>1 then
            call FieldChat(udg_DeltaLeader,3.0, "|cfff3ad001Lt. Rasczak|r : Wait..who the hell are these people?" )
            call PolledWait(3.)
            call GeneralText(5.0, "|cfff3ad00Marine|r : Civilians from the nearby town. We got to evacuate them. They'll be following your convoy out." )
            call PolledWait(6.)
        endif
        call GeneralText(4.0, "|cfff3ad00Marine|r : That smell.. What the heck are in those convoys?" )
        call PolledWait(2.)
        call FieldChat(udg_DeltaLeader,4.0, "|cfff3ad001Lt. Rasczak|r : You don't want to know." )
        call PlatoonReinforcements.execute()
        call PolledWait(17.0*udg_Pace)
        if GetRandomInt(0,1)==1 then
            call SpawnRectRandom(gg_rct_SpawnSouthWaterfall,Player(14),'n005',udg_Spawn)
            call SpawnRectRandom(gg_rct_SpawnWestTop,Player(13),'n005',udg_Spawn)
            call GankMarine(Player(14),'u005',2+udg_Spawn,1700,2500)
        endif
        call PolledWait(10.0*udg_Pace)
        set g = CreateGroup()
        call GroupEnumUnitsOfPlayer(g, Player(9), null)
        set i = 0
        loop
            set u=FirstOfGroup(g)
            exitwhen u==null
            if GetUnitPointValue(u)==98 then
                call ShiftMoveSpeed(u,-22.0)
                if GetRandomInt(0,5)==1 and i==0 then
                    set i=1
                    if GetRandomInt(0,1)==1 then
                        call FieldChat(u, 5.0, "|cfff3ad00Civilian|r : We should have stayed in town!" )
                    else
                        call FieldChat(u, 5.0, "|cfff3ad00Civilian|r : I don't want to die here!" )
                    endif
                endif
            endif
            call GroupRemoveUnit(g,u)
        endloop
        call DestroyGroup(g)
        call RandomSpawn('n002',1,udg_Spawn) //Devourer
        call RandomSpawn('n00K',1,udg_Spawn) //Blood Hound
        call RandomSpawn('u005',2,udg_Spawn) //Gargolye
        call GankMarine(Player(13),'n005',udg_Spawn,1600,2000)
        call SpawnRectRandom(gg_rct_SpawnEastCave,Player(14),'n00M',udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnNorthWaterfall,Player(13),'n00M',udg_Spawn)
        call RandomSpawn('nsw1',1,1+udg_Spawn) //Stalker
        call PolledWait(2.)
        call FieldChat(udg_DeltaLeader, 3.5, "|cfff3ad001Lt. Rasczak|r : Iron Hand, this is Rasczak. Over. Iron Hand, this is Rasczak. Over." )
        call PolledWait(2.)
        call GeneralText(3.0, "|cfff3ad00Battalion|r : Iron Hand." )
        call PolledWait(2.)
        call FieldChat(udg_DeltaLeader, 4.0, "|cfff3ad001Lt. Rasczak|r : We are being attacked, but we'll be out of here as soon as we can. Over." )
        call PolledWait(3.)
        call GeneralText(3.0, "|cfff3ad00Battalion|r : What is the status of package? Over." )
        call PolledWait(2.)
        call FieldChat(udg_DeltaLeader, 6.0, "|cfff3ad001Lt. Rasczak|r : Internal temperature in Container 4 is showing fluctuations, but nothing above normal. ETA 10 minutes before package reaches dust off point. Over." )
        call GankMarine(Player(13),'n005',5+udg_Spawn,1600,2000)
        call GankMarine(Player(14),'u005',2+udg_Spawn,1700,2500)
        call SetPopperExplosionControlEnabled(true)
        if udg_SquadXP>80000 then
            call GankMarine(Player(14),'u00U',udg_Spawn,2500,3500) //Popper sequence
            call GankMarine(Player(13),'u00U',udg_Spawn,4000,5500)
        else
            call RandomSpawn('u005',1,udg_Spawn)
            call RandomSpawn('nsw1',2,udg_Spawn)
            call SpawnRectRandom(gg_rct_SpawnNorthWaterfall,Player(14),'n005',udg_Spawn)
        endif
        call RandomSpawn('n002',1,udg_Spawn) //Devourer
        call PolledWait(5.)
        if udg_SquadXP>80000 then
            call FieldChat(udg_DeltaLeader, 5.0, "|cfff3ad001Lt. Rasczak|r : Poppers..! Spread out!" )
        endif
        call PolledWait(5.)
        if GetRandomInt(0,50)==1 then
            call GankMarine(Player(13),'n00W',2+udg_Spawn,1800,3500)
            call GankMarine(Player(13),'n00W',2+udg_Spawn,1800,3500)
        endif
        call FieldChat(udg_DeltaLeader, 5.0, "|cfff3ad001Lt. Rasczak|r : Stay in formation and clear the way for those trucks!" )
        call RandomSpawn('u00L',3,udg_Spawn) //Blink
        call SpawnRectRandom(gg_rct_SpawnSouthCave,Player(13),'n005',2*udg_Spawn)
        call SpawnRectRandom(gg_rct_SpawnEastCave,Player(13),'n005',udg_Spawn)
        call GankMarine(Player(13),'n005',udg_Spawn,1600,2000)
		
		if i2 >= 2 then
			call FieldChat(udg_DeltaLeader, 5.0, "|cfff3ad001Lt. Rasczak|r : Hey, keep an eye on those civilians!" )
		endif

        call PolledWait(15.)
        call SpawnShredder()

        call RandomSpawn('u00L',3,IMinBJ(3,udg_Spawn)) //Blink
        call RandomSpawn('nsw1',2,udg_Spawn)
        call RandomSpawn('u005',2,udg_Spawn)
        call RandomSpawn('u000',2,udg_Spawn) //Futty
        call GankMarine(Player(14),'u000',3+udg_Spawn,1700,2000)
        if udg_SquadXP>70000 and GetRandomInt(0,1)==1 then
            call GankMarine(Player(13),'n009',udg_Spawn,1700,2500)
        endif
        if udg_SquadXP>95000 and GetRandomInt(0,1)==1 then
            call GankMarine(Player(13),'n00A',udg_Spawn,1700,2500)
        endif
        call PolledWait(4.)
        call FieldChat(udg_DeltaLeader, 5.0, "|cfff3ad001Lt. Rasczak|r : Tell those drivers to step on it! We're in a fucking kill zone." )
        call PolledWait(20.)
        call RandomSpawn('n002',1,udg_Spawn) //Devourer
        call RandomSpawn('u005',2,udg_Spawn)
        call GankMarine(Player(13),'n005',udg_Spawn,1300,2800)
		
		if udg_Ogilvy != null and GetWidgetLife(udg_Ogilvy) > 0.405 then
			call FieldChat(udg_Ogilvy, 5.0, "|cfff3ad00Ogilvy|r : We're almost there!" )
		endif
        
        call PolledWait(20.)
        call GankMarine(Player(14),'u005',udg_Spawn,1700,2500)
        call RandomSpawn('u000',1,udg_Spawn) //Futty
        call SetPopperExplosionControlEnabled(false)
        set g = null
    endfunction

    private function ConvoyExfiltrationConditions takes nothing returns boolean
        return GetUnitTypeId(GetTriggerUnit())=='hrdh'
    endfunction

    private function ConvoyExfiltration takes nothing returns nothing
        local integer i = 1
        local integer civilians = 0
        local group g
        local unit u
        call RemoveUnit(GetTriggerUnit())
        set udg_ConvoyClear = udg_ConvoyClear+1
        if udg_ConvoyClear==3 then
            if udg_Mode==1 then
                call ALICEText(10.0, "Convoy secured. |cff1e90ff75 XP|r awarded to all Marines." )
                loop
                    exitwhen i>udg_InitialPlayers
                    if GetPlayerSlotState(ConvertedPlayer(udg_Sorted[i]))!=PLAYER_SLOT_STATE_LEFT then
                        set udg_Experience[udg_Sorted[i]] = udg_Experience[udg_Sorted[i]] + 75
                    endif
                    set i=i+1
                endloop
            endif
            call PolledWait(2.)
            set g = CreateGroup()
            call GroupEnumUnitsOfPlayer(g,Player(9),null)
            loop
                set u=FirstOfGroup(g)
                exitwhen u==null
                if GetUnitPointValue(u)==98 then
                    set civilians=civilians+1
                endif
                call GroupRemoveUnit(g,u)
            endloop
            call DestroyGroup(g)
            if civilians>0 and udg_Mode==1 then
                call StartSound(gg_snd_Thanks)
                if civilians==1 then
                    call ALICEText(10.0, "1 Civilian alive. |cff1e90ff10 XP|r awarded to all Marines." )
                else
                    call ALICEText(10.0, I2S(civilians) + " Civilians alive. |cff1e90ff" + I2S(civilians*10) + " XP|r awarded to all Marines." )
                endif
                if civilians>8 then
                    set udg_MissionsCompleted=udg_MissionsCompleted+1
                endif
                set i=1
                loop
                    exitwhen i>udg_InitialPlayers
                    set udg_Experience[udg_Sorted[i]] = udg_Experience[udg_Sorted[i]] + civilians*10
                    set i=i+1
                endloop
            endif
            call PolledWait(1.)
            if udg_Players>0 then
                call ExecuteWave6()
            endif
        elseif udg_ConvoyClear==1 then
            call PolledWait(20.)
            if udg_ConvoyClear==1 and udg_ConvoyLoss<3 and udg_CurrentWave==5 and udg_Players>0 then
                call ExecuteWave6()
                //call Wave6a.execute()
            endif
        endif
        set g = null
    endfunction



    // ============================================
    
   

    private function ConvoyLossConditions takes nothing returns boolean
        return GetUnitTypeId(GetTriggerUnit())=='hrdh'
    endfunction

    private function ConvoyLoss takes nothing returns nothing
        set udg_ConvoyLoss = udg_ConvoyLoss + 1
        if GetTriggerUnit()==udg_SquadLeader[1] then
            set udg_SquadLeader[1] = GroupPickRandomUnit(GetUnitsOfTypeIdAll('hrdh'))
        endif
        if udg_ConvoyLoss==3 then
            if udg_Players<1 and udg_CurrentWave!=5 then
                return
            endif
            set udg_IntenseSpawn = 1
            call SetProximitySpawnEnabled(true)
            call GeneralText(10.0, "|cfff3ad00Battalion|r : The convoy has sustained heavy losses." )
            call RandomSpawn('uabo',1,1) //Flesh Composite
            call PolledWait(180.0*udg_Pace)
            call DisableTrigger(ConvoyGuardAITrigger)
            call SetProximitySpawnEnabled(false)
            set udg_IntenseSpawn = 0
            if udg_Players>0 and udg_CurrentWave==5 then
                call ExecuteWave6()
                //call Wave6a.execute()
            endif
        elseif udg_ConvoyLoss==5 then
            if GetWidgetLife(udg_DeltaLeader)>0.405 then
                set udg_SquadLeader[1] = udg_DeltaLeader
                set udg_AITarget = udg_DeltaLeader
            else
                set udg_SquadLeader[1] = GroupPickRandomUnit(GetUnitsOfTypeIdAll('hrdh'))
                set udg_AITarget = GroupPickRandomUnit(GetUnitsOfTypeIdAll('hrdh'))
            endif
            if udg_ConvoyRoute==1 then
                set udg_ConvoyProgress=9
            elseif udg_ConvoyRoute==2 then
                set udg_ConvoyProgress=11
            elseif udg_ConvoyRoute==3 then
                set udg_ConvoyProgress=7
            endif
            if GetRandomInt(0,1)==1 then
                call FieldChat(udg_DeltaLeader, 5.0, "|cfff3ad001Lt. Rasczak|r : This is a cluster fuck! Leave this sector immediately." )
            else
                call FieldChat(udg_DeltaLeader, 5.0, "|cfff3ad001Lt. Rasczak|r : We're in a fubar situation. Let's get out of here." )
            endif
        endif
    endfunction

    private function ConvoyGuardAI takes nothing returns nothing
        local integer i = 1
        local real dx
        local real dy
        local unit marine
        local group Convoy = GetUnitsOfPlayerAll(Player(9))

        if GetRandomInt(0,2)==1 then
            call ForGroup( Convoy, function CPathOrder )
        endif
		
        if GetWidgetLife(udg_SquadLeader[1])>0.405 then
            set udg_SquadEnum = 1
            set marine = GroupPickRandomUnit(udg_InfantryGroup[1])
            set dx = GetUnitX(marine) - GetUnitX(udg_SquadLeader[1])
            set dy = GetUnitY(marine) - GetUnitY(udg_SquadLeader[1])
            if GetUnitTypeId(udg_SquadLeader[1])!='hrdh' then
                call ForGroup( udg_InfantryGroup[1], function CPathGather )
                call ForGroup( udg_InfantryGroup[2], function CPathGather )
            endif
            if dx*dx+dy*dy>=562500.0 then //750 distance
                call ForGroup( udg_InfantryGroup[1], function CPathGather )
            else
                call ForGroup( udg_InfantryGroup[1], function CPathGuard )
            endif
            set marine = GroupPickRandomUnit(udg_InfantryGroup[2])
            set dx = GetUnitX(marine) - GetUnitX(udg_SquadLeader[1])
            set dy = GetUnitY(marine) - GetUnitY(udg_SquadLeader[1])
            if dx*dx+dy*dy>=250000.0 then //500 distance
                call ForGroup( udg_InfantryGroup[2], function CPathGather )
            else
                call ForGroup( udg_InfantryGroup[2], function CPathOrder )
            endif
        endif
		
        call DestroyGroup(Convoy)
        set Convoy = null
        set marine = null
    endfunction

    private function init takes nothing returns nothing
        local trigger t
        
        set t = CreateTrigger()
        call TriggerAddCondition( t, Condition( function ConvoyRouteConditions ) )
        call TriggerAddAction( t, function ConvoyRoute )
        set ConvoyRouteTrigger = t

        set t = CreateTrigger()
        call TriggerAddCondition( t, Condition( function ConvoyLossConditions ) )
        call TriggerAddAction(t,function ConvoyLoss)
        set ConvoyLossTrigger = t

        set t = CreateTrigger()
        call TriggerAddCondition( t, Condition(function ConvoyExfiltrationConditions) )
        call TriggerAddAction(t,function ConvoyExfiltration)
        set ConvoyExfiltrationTrigger = t 
        
        set t = CreateTrigger()
        call TriggerAddAction(t,function ConvoyGuardAI)
	    call TriggerRegisterTimerEvent(t,4.0,true)
		call DisableTrigger(t)
        set ConvoyGuardAITrigger = t
        
        call RegisterFunction("Wave5a", function Wave5a)
    endfunction

endlibrary